<?php
App::uses('AppController', 'Controller');
/**
 * SalesDrugs Controller
 *
 * @property SalesDrug $SalesDrug
 */
class SalesDrugsController extends AppController {

/**
 * index method
 *
 * @return void
 */
	public function index() {
				

		$this->SalesDrug->recursive = 0;
		$this->set('salesDrugs', $this->paginate());
		//var_dump($this->paginate());
		if ($this->request->is('post')) {

			$data = $this->request->data['Data']['SalesDrug']['date'];
			$data1 = $this->request->data['Data']['SalesDrug']['date1'];
	    	$sales = $this->SalesDrug->find("all", array('conditions' => array('SalesDrug.date BETWEEN ? AND ?' => array($data, $data1))));
			$this->set('salesDrugs',$sales);
		}
	}

/**
 * view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function view($id = null) {
		$this->loadModel('Item');
		$this->SalesDrug->id = $id;
		if (!$this->SalesDrug->exists()) {
			throw new NotFoundException(__('Invalid sales drug'));
		}
		$this->set('salesDrug', $this->SalesDrug->read(null, $id));
		$itens  = $this->Item->find('all', array('conditions' => array('sales_drug_id' => $id)));
		$item = array();
		$price  = 0;
		foreach ($itens as $key => $value) {
			$item[$value['Item']['drug_id']] = $value;
			$price += $value['Item']['price']*$value['Item']['quantity'];
		}
		$this->set('price', $price);
		$this->set('items', $item);
	}

/**
 * add method
 *
 * @return void
 */
	public function add() {
		$this->loadModel('Item');
		if ($this->request->is('post')) {
			$this->SalesDrug->create();
			if ($this->SalesDrug->save($this->request->data)) {
				$this->Session->setFlash(__('The sales drug has been saved'));
				$drugs = array();
				foreach ($this->request->data['drugs_in'] as $value) {
					$drugs[] = array('Item' => array('sales_drug_id' => $this->SalesDrug->id,'drug_id' => $value, 'quantity' => $this->request->data['quantity'][$value], 'price' => $this->request->data['price'][$value]));
				}
				$this->Item->saveMany($drugs);
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The sales drug could not be saved. Please, try again.'));
			}
		}

		$users = $this->SalesDrug->User->find('list');
		$drugs = $this->SalesDrug->Drug->find('list');
		$this->set(compact('users', 'drugs'));
	}

/**
 * edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function edit($id = null) {
		$this->SalesDrug->id = $id;
		if (!$this->SalesDrug->exists()) {
			throw new NotFoundException(__('Invalid sales drug'));
		}
		if ($this->request->is('post') || $this->request->is('put')) {
			if ($this->SalesDrug->save($this->request->data)) {
				$this->Session->setFlash(__('The sales drug has been saved'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The sales drug could not be saved. Please, try again.'));
			}
		} else {
			$this->request->data = $this->SalesDrug->read(null, $id);
		}
		$users = $this->SalesDrug->User->find('list');
		$drugs = $this->SalesDrug->Drug->find('list');
		$this->set(compact('users', 'drugs'));
	}

/**
 * delete method
 *
 * @throws MethodNotAllowedException
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function delete($id = null) {
		if (!$this->request->is('post')) {
			throw new MethodNotAllowedException();
		}
		$this->SalesDrug->id = $id;
		if (!$this->SalesDrug->exists()) {
			throw new NotFoundException(__('Invalid sales drug'));
		}
		if ($this->SalesDrug->delete()) {
			$this->Session->setFlash(__('Sales drug deleted'));
			$this->redirect(array('action' => 'index'));
		}
		$this->Session->setFlash(__('Sales drug was not deleted'));
		$this->redirect(array('action' => 'index'));
	}
}
